<template>
  <n-dropdown
    :options="exportOptions"
    placement="bottom-start"
    trigger="click"
    @select="handleExportSelect"
  >
    <n-button class="mr-2">
      <template #icon>
        <n-icon size="22">
          <DocumentExport />
        </n-icon>
      </template>
      {{ "导出" }}
    </n-button>
  </n-dropdown>

  <n-button type="primary" @click="handleImport">
    <template #icon>
      <n-icon>
        <DocumentImport />
      </n-icon>
    </template>
    {{ "导入" }}
  </n-button>
</template>

<script setup>
import { onMounted } from "vue";
import { DocumentImport, DocumentExport } from "@vicons/carbon";
// import { t } from "@/language";
import { Export } from "@/core/exporters/export";

const exportClass = new Export();

const exportOptions = [
  {
    label: "导出几何体",
    key: "exportGeometry",
  },
  {
    label: "导出物体",
    key: "exportObject",
  },
  {
    label: "导出场景",
    key: "exportScene",
  },
  // {
  //   label: "导出全场景",
  //   key: "exportSceneAll",
  // },
  {
    type: "divider",
    key: "d1",
  },
  {
    label: "导出格式",
    key: "exportFormat",
    children: [
      {
        label: "DAE",
        key: "exportDae",
      },
      {
        label: "GLB",
        key: "exportGlb",
      },
      {
        label: "GLTF",
        key: "exportGltf",
      },
      {
        label: "OBJ",
        key: "exportObj",
      },
      {
        label: "PLY",
        key: "exportPly",
      },
      {
        label: "PLY(二进制)",
        key: "exportPlyBinary",
      },
      {
        label: "STL",
        key: "exportStl",
      },
      {
        label: "STL(二进制)",
        key: "exportStlBinary",
      },
      {
        label: "USDZ",
        key: "exportUSDZ",
      },
    ],
  },
];

// import { SetMaterialColorCommand } from "@/core/commands/SetMaterialColorCommand";
// import { SetMaterialValueCommand } from "@/core/commands/SetMaterialValueCommand";
import * as THREE from "three";

import { Color } from "three";

function handleImport() {
  const form = document.createElement("form");
  form.style.display = "none";
  document.body.appendChild(form);

  const fileInput = document.createElement("input");
  fileInput.multiple = true;
  fileInput.type = "file";
  fileInput.addEventListener("change", function () {
    window.editor.loader.loadFiles(fileInput.files, undefined, () => {
      form.reset();
      setTimeout(() => {
        /*
        const box = new THREE.Box3().setFromObject(window.editor.selected);
        const center = box.getCenter(new THREE.Vector3());
        window.editor.selected.children.forEach((object) => {
          // console.log(center);
          // //将坐标归零
          // object.position.sub(center);

          object.children.forEach((child) => {
            // console.log("**------,", child);
            // const box = new THREE.Box3().setFromObject(window.viewport.scene);
            // const center = box.getCenter(new THREE.Vector3());
            // console.log(center);
            //将坐标归零
            child.position.sub(center);
            //y轴坐标全部在当前基础上加一半
            child.position.y =
              child.position.y + box.getSize(new THREE.Vector3()).y / 2;
          });
        });
        */
        /*
        //将所有的模型的材质设置为默认材质
        const children = window.editor.selected.children;


        children.forEach((object) => {




          object.traverse((child) => {
            if (child.isMesh) {
              const currentMaterialSlot = 0;
              const material = window.editor.getObjectMaterial(
                child,
                currentMaterialSlot
              );
              // console.log("material", material);
              // console.log(material["color"].getStyle());
              // rgb(202,202,202)
              // rgb(0,0,0)
              if (material["color"].getStyle() === "rgb(0,0,0)") {
                const newColor = new Color("rgb(202, 202, 202)").getHex();
                window.editor.execute(
                  new SetMaterialColorCommand(
                    child,
                    "color",
                    newColor,
                    currentMaterialSlot
                  )
                );
                window.editor.execute(
                  new SetMaterialValueCommand(
                    child,
                    "roughness",
                    "0.6678069829940796",
                    currentMaterialSlot
                  )
                );
                window.editor.execute(
                  new SetMaterialValueCommand(
                    child,
                    "metalness",
                    "0",
                    currentMaterialSlot
                  )
                );
              }

              // console.log("material", material);
              // window.editor.execute(
              //   new SetMaterialColorCommand(
              //     object,
              //     "color",
              //     newColor,
              //     currentMaterialSlot
              //   )
              // );



            }
          });
        });
        */
      }, 3000);
    });
  });
  form.appendChild(fileInput);

  fileInput.click();
}

// 导入模型, 但不立即加载到场景中, 而是将模型存放到列表中, 等待用户选择加载
// function handleImport() {

// }

function handleExportSelect(key) {
  exportClass.init(key);
}

onMounted(() => {});
</script>

<style scoped lang="less"></style>
